package websocket;



import tech.gusavila92.websocketclient.WebSocketClient;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by donar on 18/1/16.
 */
public class Client {
    private WebSocketClient webSocketClient;

    private void createWebSocketClient() {
        URI uri;
        try {
            uri = new URI("ws://spe-frontier.snssdk.com/ws/v2?aid=13&device_id=34996452376&access_key=efcfd09798ddaa98cd0bf609aa5cd8ca&fpid=90001&sdk_version=2&iid=22859550049&sid=496b183ba7d77e2450ce105d14462ac6&pl=0&ne=4&version_code=654&version=1");
        }
        catch (URISyntaxException e) {
            e.printStackTrace();
            return;
        }

        webSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen() {
                System.out.println("onOpen");
                webSocketClient.send("Hello, World!");
            }

            @Override
            public void onTextReceived(String message) {
                System.out.println(message);
                System.out.println("onTextReceived");
            }

            @Override
            public void onBinaryReceived(byte[] data) {
                if(!new String(data).contains("*"))return;
                String str = (new String(data)).replaceAll("[^\\u0020-\\u9FA5]","");
                int start = str.indexOf("*")+2;
                String[] list = str.substring(start).replace("PB","").split("B");
                if(list.length<4){
                    String str2 = (new String(data).replaceAll("\n","B")).replaceAll("[^\\u0020-\\u9FA5]","");
                    int start2 = str2.indexOf("*")+2;
                    list = str2.substring(start).replace("PB","").split("B");
                }
                String question = list[0];
                String option1 = list[1];
                String option2 = list[2];
                String option3 = list[3];
                System.out.println("onBinaryReceived");
            }

            @Override
            public void onPingReceived(byte[] data) {
                System.out.println("onPingReceived");
            }

            @Override
            public void onPongReceived(byte[] data) {
                System.out.println("onPongReceived");
            }

            @Override
            public void onException(Exception e) {
                System.out.println(e.getMessage());
            }

            @Override
            public void onCloseReceived() {
                System.out.println("onCloseReceived");
            }
        };

        webSocketClient.setConnectTimeout(10000);
        webSocketClient.setReadTimeout(60000);
        webSocketClient.enableAutomaticReconnection(5000);
        webSocketClient.connect();
    }

    public static void main(String[] args) {
        //new Client().createWebSocketClient();

    }
}
